package com.base.system.base.http;

import cn.hutool.core.util.StrUtil;
import com.base.system.enums.HttpStatus;
import com.base.system.exception.HttpException;
import lombok.Getter;

@Getter
public class HttpResult<T> {
    private Integer status;
    private Boolean ok;
    private T data;
    private String msg;

    private HttpResult(Integer status, Boolean ok, T data, String msg) {
        this.status = status;
        this.ok = ok;
        this.data = data;
        this.msg = msg;
    }

    public static HttpResult ok() {
        return new HttpResult(HttpStatus.success.getCode(), true, null, HttpStatus.success.getNote());
    }

    public static HttpResult ok(Object o) {
        return new HttpResult(HttpStatus.success.getCode(), true, o, HttpStatus.success.getNote());
    }

    public static HttpResult ok(Object o, String msg) {
        return new HttpResult(HttpStatus.success.getCode(), true, o, msg);
    }

    public static HttpResult faild() {
        return faild(HttpStatus.faild);
    }

    public static HttpResult faild(HttpStatus status) {
        return faild(status, null);
    }

    public static HttpResult faild(HttpStatus status, String msg) {
        return faild(status, null, msg);
    }

    public static HttpResult faild(HttpException e) {
        return faild(HttpStatus.getObjByCode(e.getCode()), e.getMsg());
    }

    public static HttpResult faild(String msg) {
        return faild(HttpStatus.bad_error, msg);
    }

    public static HttpResult faild(HttpStatus status, Object backValue, String msg) {
        return new HttpResult(status.getCode(), false, backValue, StrUtil.isEmpty(msg) ? status.getNote() : msg);
    }
}
